package com.qianlou.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.iv.RandomIvGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Jasypt 加密属性解析器配置类
 * 用于提供自定义的 encryptablePropertyResolver Bean
 * 注意：如果 Jasypt 已经默认注册了该 Bean，可根据实际情况决定是否保留此配置
 */
@Configuration
public class JasyptConfig {

    public static final String ENV_PASSWORD_KEY_NAME = "jasypt.encryptor.password";

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        return getEncryptor();
    }

    public static StringEncryptor getEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(System.getenv(ENV_PASSWORD_KEY_NAME));
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setIvGenerator(new RandomIvGenerator());
        config.setKeyObtentionIterations("1000");
        config.setPoolSize(1);
        encryptor.setConfig(config);
        return encryptor;
    }

}